//
//  EditBirViewController.m
//  RENTVISITOR
//
//  Created by wonder on 2017/8/29.
//  Copyright © 2017年 UNN. All rights reserved.
//

#import "EditBirViewController.h"
#import "RentVieitor.h"

@interface EditBirViewController ()<UITextFieldDelegate>

@end

@implementation EditBirViewController {
    
    UITextField *dateText;
    UIDatePicker *datePicker;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"生日";
    self.view.backgroundColor = BackgroundColor;
    [self creatLeftView];
    [self creatView];
}

- (void)creatLeftView {
    
    // 自定义导航栏左侧按钮
    UIButton * leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(0, 0, 10,15 );
    [leftBtn setBackgroundImage:[UIImage imageNamed:@"箭头左"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * leftItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    // 自定义导航栏左侧按钮
    UIButton * rightBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    rightBtn.frame = CGRectMake(0, 0, 50, 20);
    [rightBtn setTitle:@"保存" forState:UIControlStateNormal];
    [rightBtn setTitleColor:[UIColor colorWithRed:99/255.0 green:99/255.0 blue:99/255.0 alpha:1] forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(saveAction) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * rightItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    self.navigationItem.rightBarButtonItem = rightItem;
    
}

- (void)creatView {
    
    UIView *bdView = [[UIView alloc] initWithFrame:CGRectMake(0, 10, KWidth, 50)];
    bdView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bdView];
    
    dateText = [[UITextField alloc] initWithFrame:CGRectMake(20, 10, KWidth-30, 30)];
    dateText.text = self.date;
    dateText.delegate = self;
    dateText.textColor = [Utils colorConvertFromString:@"#333333"];
    dateText.font = [UIFont systemFontOfSize:15];
    
    UIButton *canceBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    canceBtn.frame = CGRectMake(0, 0, 15, 15);
    [canceBtn setBackgroundImage:[UIImage imageNamed:@"关闭"] forState:UIControlStateNormal];
    [canceBtn addTarget:self action:@selector(canceAction) forControlEvents:UIControlEventTouchUpInside];
    dateText.rightView = canceBtn;
    dateText.rightViewMode = UITextFieldViewModeAlways;
    [bdView addSubview:dateText];
    
    datePicker = [[UIDatePicker alloc]initWithFrame:CGRectMake(0, KHeight-216, self.view.frame.size.width, 216)];
    datePicker.hidden = YES;
    datePicker.datePickerMode = UIDatePickerModeDate;
    datePicker.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.view addSubview:datePicker];
    
    [datePicker addTarget:self action:@selector(dateChanged:) forControlEvents:UIControlEventValueChanged ];//重点：UIControlEventValueChanged
    
   
}

-(void)dateChanged:(id)sender{
    
    //NSDate格式转换为NSString格式
    NSDate *pickerDate = [datePicker date];// 获取用户通过UIDatePicker设置的日期和时间
    NSDateFormatter *pickerFormatter = [[NSDateFormatter alloc] init];// 创建一个日期格式器
    [pickerFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString *dateString = [pickerFormatter stringFromDate:pickerDate];
    
    dateText.text = dateString;
    
    
    
}


//取消
- (void)canceAction {
    
    dateText.text = @"";
}

//保存
- (void)saveAction {
    
    datePicker.hidden = YES;
    if (dateText.text.length == 0) {
        
        [APPDELEGATE showPro:@"请选择时间"];
        return;
    }
    NSString *birthday_yyyy = [dateText.text substringWithRange:NSMakeRange(0, 4)];
    NSString *birthday_mm = [dateText.text substringWithRange:NSMakeRange(5, 2)];
    //转换10以下的数字
    if ([birthday_mm integerValue] <10) {
        birthday_mm = [NSString stringWithFormat:@"%ld",[birthday_mm integerValue]];
    }
    
    
    
    NSString *birthday_dd = [dateText.text substringWithRange:NSMakeRange(8, 2)];
    //转换10以下的数字
    if ([birthday_dd integerValue] <10) {
        birthday_dd = [NSString stringWithFormat:@"%ld",[birthday_dd integerValue]];
    }
    NSDictionary *dataDic = User_listsValue;
    NSString *url = [NSString stringWithFormat:@"%@member/info/birthday",Main_Url];
    NSString *number = dataDic[@"number"];
    NSString *token = dataDic[@"token"];
    NSString *md5String = [NSString stringWithFormat:@"%@%@%@%@%@%@%@",birthday_dd,birthday_mm,birthday_yyyy,number,token,APPKEY_URL,url];
    
    //32位小写
    
    //    NSString *nickname = [nameText.text stringByAddingPercentEscapesUsingEncoding:
    //                          NSUTF8StringEncoding];
    
    md5String = [LCMD5Tool MD5ForLower32Bate:md5String];
    NSDictionary *param = @{
                            @"number":number,
                            @"token":token,
                            @"birthday_yyyy":birthday_yyyy,
                            @"birthday_mm":birthday_mm,
                            @"birthday_dd":birthday_dd,
                            @"sign":md5String
                            };
    [AFHttpTool requestWihtUrl:url params:param success:^(id response) {
        
        
        if ([response[@"code"] isEqualToString:@"10000"]) {
            
            [APPDELEGATE showEditSuccess:@"修改成功"];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"update" object:nil userInfo:nil];
        }
        
    } failure:^(NSError *err) {
        
        
    }];

}

//结束编辑
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    datePicker.hidden = YES;
}

//显示时间选择器
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    
    datePicker.hidden = NO;
    return NO;
}


- (void)backAction {
    
    [self.navigationController popViewControllerAnimated:YES];
}


@end
